<?php

class AuthenticationController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	//$this->_redirect('authentication/login');
    }

    public function loginAction()
    {
    	//pt a nu se auth de 2 ori
    	if(Zend_Auth::getInstance()->hasIdentity())
    	{
    		$this->_redirect('user/index');
    	}
    	
    	//check how is the request
    	$request = $this->getRequest();
    	$form = new Application_Form_Login();

    	if( $request->isPost() )
    	{
    		if($form->isValid($this->_request->getPost()))
    		{
		    	$authAdapter = $this->getAuthAdapter();
		    	
		    	$username = $form->getValue('username');
		    	$password = $form->getValue('password');
		
		    	//put input values in authAdapter
		    	$authAdapter->setIdentity($username)
		    				->setCredential($password);
		    	
		    	//do authentication throw zend_instance
		    	$auth = Zend_Auth::getInstance();
		    	$result = $auth->authenticate($authAdapter);
		    	
		    	if( $result->isValid() )
		    	{
		    		//grab user details from table using zendAuthAdapter
		    		$identity = $authAdapter->getResultRowObject();
		    		
		    		//use zend auth instance to write that identity to persistence storage to can be access in other parts of the application
		    		$authStorage = $auth->getStorage();
		    		$authStorage->write($identity);
		    		//$this->_redirect(Zend_Controller_Front::getInstance()->getBaseUrl().'authentication/index');
		    		$this->_redirect('user/index');
		    	}
		    	else
		    	{
		    		$errorMessage = $errorMessage."<br/> - Invalid username or password! Please try again!";
		    		$this->view->errorMessage = $errorMessage;
		    	}
    		}
    		else
    		{
    			$messages = $form->getMessages();
    			if(isset($messages['username']))
    			{
    				$errorMessage = $errorMessage."<br/> - Username value is required and can't be empty!";
    				$this->view->errorMessage = $errorMessage;
    			}
    			if(isset($messages['password']))
    			{
    				$errorMessage = $errorMessage."<br/> - Password value is required and can't be empty!";
    				$this->view->errorMessage = $errorMessage;
    			}
       		}		
    	}
    	
    	$this->view->loginForm = $form;
    	
    }
    
    private function getAuthAdapter()
    {
       	$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
    	$authAdapter->setTableName('users')
    				->setIdentityColumn('username')
    				->setCredentialColumn('password');
    				//->setCredentialTreatmenet('');
    				
    	return 	$authAdapter;
    }

    public function logoutAction()
    {
        //erasing all session data stored
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('authentication/index');
    }

    public function registerAction()
    {
    	$usersTbl = new Application_Model_UsersTbl();
    	$form = new Application_Form_Registration();
    	$this->view->registerForm = $form;
    	
    	if($this->getRequest()->isPost()){
    		if($form->isValid($_POST)){
    			$data = $form->getValues();
    			if( $data['password'] != $data['confirmPassword'] ){
    				$errorMessage = $errorMessage."<br/> - Password and confirm password don't match!";
    				$this->view->errorMessage = $errorMessage;
    				return;
    			}
    			if( $usersTbl->checkUnique($data['username'])){
    				$errorMessage = $errorMessage."<br/> - Name already taken. Please choose another one!";
    				$this->view->errorMessage = $errorMessage;
    				return;
    			}
    			unset($data['confirmPassword']);
    			$usersTbl->insert($data);
    			$this->_redirect('authentication');
    		}
    		else
    		{
    			$messages = $form->getMessages();
    			if(isset($messages['email']))
    			{
    				$errorMessage = $errorMessage."<br/> - Email value is required and can't be empty!";
    				$this->view->errorMessage = $errorMessage;
    			}
    			if(isset($messages['username']))
    			{
    				$errorMessage = $errorMessage."<br/> - Username value is required and can't be empty!";
    				$this->view->errorMessage = $errorMessage;
    			}
    			
    			if(isset($messages['password']))
    			{
    				$errorMessage = $errorMessage."<br/> - Password value is required and can't be empty!";
    				$this->view->errorMessage = $errorMessage;
    			}
    			
       		}		
    	}
       
    }


}







